package com.xiaren.ui;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.plaf.basic.BasicBorders;

import com.xiaren.biz.MenuItemTools;
import com.xiaren.biz.PublicFunction;
import com.xiaren.common.IConstant;
import com.xiaren.common.ImageManager;
import com.xiaren.common.ObjectConstant;
import com.xiaren.dao.BaseDao;
import com.xiaren.dao.FileDao;

/**
 * 用于添加MainFrame中的事件
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public abstract class AbstractFrame extends JFrame {

	/**
	 * 用于数据访问层
	 */
	public BaseDao baseDao = new FileDao();

	/**
	 * 菜单栏和菜单按钮
	 */
	public JMenuBar mBar = new JMenuBar();
	public JMenu thefile = new JMenu("File");
	public JMenu theedit = new JMenu("Eide");
	public JMenu run_file = new JMenu("Run file");

	public JMenuItem newf = new JMenuItem("New");
	public JMenuItem open = new JMenuItem("Open");
	public JMenuItem close = new JMenuItem("Close");
	public JMenuItem quit = new JMenuItem("Exit");
	/**
	 * 主界面title
	 * @param title
	 * @throws HeadlessException
	 */
	public AbstractFrame(String title) throws HeadlessException {
		this.setTitle(title);
	}

	protected void initCommpent() {

		/******** JMenuBar***************begin *****/
		thefile.setMnemonic('F');

		JMenuItem newf = new JMenuItem("New");
		JMenuItem open = new JMenuItem("Open");
		JMenuItem close = new JMenuItem("Close");
		JMenuItem quit = new JMenuItem("Exit");

		newf.setMnemonic('N');//菜单的某个字母加下划线(即是助记符)
		open.setMnemonic('O');
		close.setMnemonic('L');
		quit.setMnemonic('X');

		newf.setAccelerator(KeyStroke.getKeyStroke('N',
				java.awt.Event.CTRL_MASK, false));
		open.setAccelerator(KeyStroke.getKeyStroke('O',
				java.awt.Event.CTRL_MASK, false));
		close.setAccelerator(KeyStroke.getKeyStroke('L',
				java.awt.Event.CTRL_MASK, false));
		quit.setAccelerator(KeyStroke.getKeyStroke('X',
				java.awt.Event.CTRL_MASK, false));

		JMenu prefMenu = new JMenu("Preferences..");
		prefMenu.setMnemonic('P');

		JMenuItem setPage = new JMenuItem("setPage");
		JMenuItem setImport = new JMenuItem("Import");
		JMenuItem setExport = new JMenuItem("Export");

		setPage.setMnemonic('S');
		setImport.setMnemonic('I');
		setExport.setMnemonic('E');

		setPage.setAccelerator(KeyStroke.getKeyStroke('S',
				java.awt.Event.CTRL_MASK, false));
		setImport.setAccelerator(KeyStroke.getKeyStroke('I',
				java.awt.Event.CTRL_MASK, false));
		setExport.setAccelerator(KeyStroke.getKeyStroke('E',
				java.awt.Event.CTRL_MASK, false));

		prefMenu.add(setPage);
		prefMenu.add(setImport);
		prefMenu.add(setExport);

		thefile.add(newf);
		thefile.add(open);
		thefile.add(close);
		thefile.addSeparator();
		thefile.add(prefMenu);
		thefile.addSeparator();
		thefile.add(quit);

		mBar.add(thefile);

		theedit.setMnemonic('E');

		JMenuItem b = new JMenuItem("B");
		JMenuItem c = new JMenuItem("C");
		JMenuItem d = new JMenuItem("D");

		b.setMnemonic('B');
		c.setMnemonic('C');
		d.setMnemonic('D');


		b.setAccelerator(KeyStroke.getKeyStroke('B', java.awt.Event.CTRL_MASK,
				false));
		c.setAccelerator(KeyStroke.getKeyStroke('C', java.awt.Event.CTRL_MASK,
				false));
		d.setAccelerator(KeyStroke.getKeyStroke('D', java.awt.Event.CTRL_MASK,
				false));

		theedit.add(ObjectConstant.menuPrint = MenuItemTools.getScreenShots());
		theedit.add(b);
		theedit.add(c);
		theedit.add(d);

		mBar.add(theedit);


		run_file.add(ObjectConstant.btnRunCmd = new JMenuItem("run Cmd"));
		mBar.add(run_file);// 运行文件
		mBar.setBorder(BasicBorders.getMenuBarBorder());
		/******** JMenuBar***************end *****/

		this.add(mBar, BorderLayout.NORTH);
		this.add(ObjectConstant.mainPanel);

	}

	protected void initLayout() {
		ObjectConstant.mainPanel = new MainJPanel(this);
		ObjectConstant.mainPanel.setLayout(new BorderLayout());
	}

	/**
	 * 初始化Run_file元素
	 */
	protected void inittoolsElement() {
		// TODO 需要修改 2016年12月24日
//		File[] fileList = ReadWriteFile.findFileList(IConstant.TOOLS_PATH);
//		if (fileList != null) {
//			for (final File file : fileList) {
//				JMenuItem jumenuitem = new JMenuItem(file.getName());
//				run_file.add(jumenuitem);
//				jumenuitem.addActionListener(new ActionListener() {
//					@Override
//					public void actionPerformed(ActionEvent e) {
//						String cmdContent = ReadWriteFile.readFile(file.getAbsolutePath());
//						try {
//							String[] arrayStr = cmdContent.split("\r\n");
//							PublicFunction.runCmdProgress(arrayStr);
//						} catch (IOException e1) {
//							e1.printStackTrace();
//						}
//					}
//				});
//			}
//		}
	}

	protected void init() {
		initLayout();
		initCommpent();
		inittoolsElement();
		ObjectConstant.titleImage = ImageManager.IMAGE_TITLE.getImage()
				.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
		this.setIconImage(ObjectConstant.titleImage);
	}

}
